from bs4 import BeautifulSoup
import requests
import time

url = '''http://guahao.zjol.com.cn/pb/957108?deptId=7195&fuzzy_deptId=0&docId=&fuzzy_docId=0'''

while True:
    try:
        r = requests.get(url)
    except Exception as e:
        print('can not read url for exception {}'.format(e))
        continue
    soup = BeautifulSoup(r.text, 'html.parser')
    found = False
    for tag in soup.find_all('li', class_='yy'):
        if tag.string == '预约':
            found = True
            break
    if found:
        print('found target, sending message by mail')
        mailUrl = "http://api.sendcloud.net/apiv2/mail/send"
        API_USER = 'hamwj1991_test_ldTCm5'
        API_KEY = 'KyUYfhuEeBoiEniB'
        params = {
            "apiUser": API_USER,
            "apiKey": API_KEY,
            "to": "hamwj1991@qq.com",
            "from": "hamwj1991@sendcloud.com",
            "fromName": "bot",
            "subject": "Notify Registration",
            "html": "lalalala"
        }
        req = requests.post(mailUrl, data=params)
        print(req.text)
        # sleep for 1 hour
        time.sleep(3600)
    else:
        print('not found @', time.asctime(time.localtime()))
        # sleep for 10 seconds
        time.sleep(10)
